/**
 * Created by xyy on 2021/3/5.
 */
function sheetProtection(){
    // alert('工作表保护修改为采用自定义保护插件，不再使用luckysheet提供的保护插件！');
    protectFormFlag = true;
    if(protectCellData.hasOwnProperty(luckysheet.getSheet().name)){
        $('#protectCellId').val(protectCellData[luckysheet.getSheet().name]);
        var data = new Array();
        data = protectCellData[luckysheet.getSheet().name].split(',');
    }

    //多窗口模式，层叠置顶
    $('#proCellSelect').show();
    layer.open({
        type: 1 //此处以iframe举例
        ,title: '受保护单元格'
        ,area: ['390px', '200px']
        ,shade: 0   //遮罩,不想显示遮罩，可以shade: 0
        ,maxmin: true   //最大最小化
        ,id: 'protectShow' //设置一个id防止重复弹出
        ,content: $('#protectForm')
        ,btn: ['确定', '取消']
        ,yes: function(){
            var key = luckysheet.getSheet().name;
            protectCellData[key] = $('#protectCellId').val();
            $('#proCellSelect').hide();
            protectFormFlag = false;
            layer.closeAll();
        }
        ,btn2: function(){
            $('#proCellSelect').hide();
            protectFormFlag = false;
            layer.closeAll();
        }
        ,cancel: function(){ 
            //右上角关闭回调
            $('#proCellSelect').hide();
            protectFormFlag = false;
            // return false //开启该代码可禁止点击该按钮关闭
        }
        ,zIndex: layer.zIndex //重点1
        ,success: function(layero){
            //层弹出后的成功回调方法
            layer.setTop(layero); //重点2
        }
    });
}

/**
 * 展开单元格区域  例 A1:B2 --> A1,B1,A2,B2
 */
function showCellArea(cellStr) {
    let result = [];
    let split = [];
    let x = [];
    let y = [];
    split = cellStr.split(':');
    x = excelIndex2RowCol(split[0]);
    y = excelIndex2RowCol(split[1]);
    for (let i = x[0]; i <= y[0]; i++) {
        for (let j = x[1]; j <= y[1]; j++) {
            var param = [j,i];
            result.push(getCellStrIndex(param));
        }
    }
    return result;
}

/**
 * 表头数组转换字符串 ，例如：[11,27] ---> AB11
 */
function getCellStrIndex(rowAndCol) {
    let colStr = numToSheetCol(rowAndCol[0]);
    let rowStr = rowAndCol[1] + 1;
    return colStr + rowStr;
}

/**
 *  表格字符串字母转数字  例 AB --> 27
 * @param index
 */
function sheetColToNum(index) {
    index = index.toUpperCase();
    var ind = 0;
    for (var i = 0; i < index.length; i++) {
        var c = index.charCodeAt(i);
        ind += (c - 64) * Math.pow(26,index.length-i-1);
    }
    return ind - 1;
}

/**
 *  数字转表格字符串字母  例 27 --> AB
 */
function numToSheetCol(index) {
    let result = '';
    for (; index >= 0; index = index/26 - 1) {
        result = String.fromCharCode(index % 26 + 65) + result;
    }
    return result;
}

/**
 *  表头字符串转换数组 ，例如：AB11 ---> [11,27]
 */
function excelIndex2RowCol(index){
    let colIndexs = [] ;
    let rowIndexs = [];
    for (var i = 0; i < index.length; i++) {
        var charCode = index.charCodeAt(i);
        if(charCode<=57 && charCode>=48){
            rowIndexs.push( charCode);
        }else{
            colIndexs.push( charCode);
        }
    }
    return [parseInt(String.fromCharCode(...rowIndexs))-1,sheetColToNum(String.fromCharCode(...colIndexs))];
}